//
//  LFHomeHotSellCell.m
//  LoveFruit
//
//  Created by 张令林 on 16/9/23.
//  Copyright © 2016年 张令林. All rights reserved.
//

#import "LFHomeHotSellCell.h"
#import "LFOrderTableViewController.h"
@interface LFHomeHotSellCell ()

@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
//精选
@property (weak, nonatomic) IBOutlet UIImageView *sortView;
//买一送一
@property (weak, nonatomic) IBOutlet UIImageView *pmdescView;
//400g/盒
@property (weak, nonatomic) IBOutlet UILabel *specificsLabel;
//购物车
@property (weak, nonatomic) IBOutlet UIButton *desBtn;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;
@property (weak, nonatomic) IBOutlet UILabel *countLabel;
//价格
@property (weak, nonatomic) IBOutlet UILabel *partnerPriceLabel;
@property (weak, nonatomic) IBOutlet UILabel *marketPriceLabel;

@property (nonatomic ,strong) LFOrderTableViewController *OrderVC;
@end

@implementation LFHomeHotSellCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
}
#pragma mark - 重写模型的set方法,设置各个控件的内容
-(void)setModel:(LFHotSellerModel *)model
{
    _model = model;
    //设置数据
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"Yosemite00"]];
    self.nameLabel.text = model.name;
    
    //精选
    if (model.is_xf == 1)
    {
        self.sortView.hidden = NO;
    }else
    {
        self.sortView.hidden = YES;
    }
    //买一送一
    if (model.pm_desc != nil)
    {
        self.sortView.hidden = NO;
    }else
    {
        self.sortView.hidden = YES;
    }
    //400g/盒
    self.specificsLabel.text = model.specifics;
    //购物车的逻辑
    [self getShopCarInfo:model];
    //价格
    self.partnerPriceLabel.text = model.price;
    self.marketPriceLabel.text = model.market_price;
}
#pragma mark - 获取购物车相关处理的方法
- (void)getShopCarInfo:(LFHotSellerModel *)model
{
    NSInteger count = model.ismix;
    [self isNumber:count];
}
- (IBAction)addBtnClick:(UIButton *)sender
{ 
    if (self.model.ismix >= 8)
    {
        [SVProgressHUD showInfoWithStatus:@"库存已不足"];
        return;
    }else
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"model" object:nil userInfo:@{@"modelkey":self.model,@"imageView":self.imgView}];
    }
    //这个是正常的逻辑,上面添加一句代码是为了演示提示
    NSInteger count = self.model.store_nums;
    self.model.ismix++;
    NSInteger count1 = self.model.ismix;
    [self isNumber:count1];
    if (count <= 0)
    {
        [SVProgressHUD showInfoWithStatus:@"库存已不足"];
        return;
    }
    self.model.store_nums--;
    
  
    
}
- (IBAction)desBtnClick:(UIButton *)sender
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"   " object:nil];
    self.model.ismix--;
    NSInteger count = self.model.ismix;
    [self isNumber:count];
    

}
//根据购物车里面的数量确定btn和label的显示
- (void)isNumber:(NSInteger)count
{
    if (count > 0)
    {
        self.desBtn.hidden = NO;
        self.countLabel.hidden = NO;
        self.countLabel.text = [NSString stringWithFormat:@"%zd",self.model.ismix];
    }
    else
    {
        self.desBtn.hidden = YES;
        self.countLabel.hidden = YES;
    }
}
@end
